package modelLayer;

import java.io.IOException;
import java.util.ArrayList;

import uiLayer.SaveToFile;
import controllerLayer.CustomerCtr;

public class SaleRecords {
	private ArrayList<Sale> offers;

	private CustomerCtr custCtr;
	private static SaleRecords instance = null;
	private int id;
	public SaleRecords() {
		offers =  new ArrayList<Sale>();
		id = offers.size();
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	public static SaleRecords getInstance()       
	{
		if(instance == null)
		{
			instance = new SaleRecords();
		}
		return instance;
	}
	public int createOffer(Customer customer,Employee employee, ArrayList<SaleLinesItem> sli)
	{
		id++;
		Sale sale = new Sale(id, customer,employee,sli );
		offers.add(sale);
		try{
			customer.addSale(sale);
		}
		catch(NullPointerException npe){
			System.out.println("Either Customer, Employee doesn't exist or no items added");
		}
		return id;
	}

	public ArrayList<Sale> getOffers() {
		return offers;
	}
	public String printOffers(String custID){
		Customer c1 = custCtr.getCustomer(custID) ;
		String ofs = new String();
		int i = 0;
		for(Sale o1:offers){
			if(o1.getCustomer().equals(c1)){
				i++;
				ofs =ofs + i +o1.toString();
			}
		}
		return ofs;
	}
	public Sale findSale(int Id){
		for(Sale sale: offers){
			if(sale.getId()==Id)return sale;
			
		}
		return null;
	}

	public int getId() {
		return id;
	}
	public void deleteSale(Sale o){
		offers.remove(o);
	}
	public void save() throws IOException{
		for(Sale s1: offers){
			SaveToFile.getInstance().save(s1.save());
	}
	}
	}



